//
//  BXError.m
//  IBoXiao_ios
//
//  Created by 于龙 on 14-4-22.
//  Copyright (c) 2014年 ___boxiao___. All rights reserved.
//

#import "FYError.h"

/**
 *  网络错误提示
 */
#define kNetWorkError404Message           @"服务器内部错误(404)，请稍后重试"

/**
 *  网络错误提示
 */
#define kNetWorkError500Message           @"服务器内部错误(500)，请稍后重试"

/**
 *  Network 未连接到服务器
 */
#define kNetWorkErrorUnReach              @"无法连接到服务器，请检查你的网络或稍后重试"


@implementation FYError
+ (FYError *)errorWithCode:(NSInteger)code Message:(NSString *)msg {
    
    FYError* error = [FYError errorWithCode:code];
    
   
    
    return error;
}

+ (FYError *)errorWithCode:(NSInteger)code {

//    NSString *msg = [NSString stringWithFormat:@"%@(%@)",kNetWorkErrorUnReach,[NSNumber numberWithInteger:code]];
    
    NSString *msg = kNetWorkErrorUnReach;
    
    switch (code) {
        case 0:
            msg = kNetWorkErrorUnReach;
            break;
            
        case 404:
            msg = kNetWorkError404Message;
            break;
            
        case 500:
            msg = kNetWorkError500Message;
            break;
            
        default:
            break;
    }
    
    FYError *error = [[FYError alloc] init];
    error.errorCode = code;
    error.errorMessage = msg;
    
    return error;
    
}

+ (FYError *)errorWithOperation:(NSURLSessionDataTask*)operation {
    
    NSString *msg = kNetWorkErrorUnReach;
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)operation.response;
    switch (response.statusCode) {
        case 0:
            msg = kNetWorkErrorUnReach;
            break;
            
        case 200:
//            msg = operation.responseObject[@"message"];;
            break;
            
        case 404:
            msg = kNetWorkError404Message;
            break;
            
        case 500:
            msg = kNetWorkError500Message;
            break;
        default:
            break;
    }
    
    FYError *error = [[FYError alloc] init];
    error.errorCode = response.statusCode;
    error.errorMessage = msg;
    
    return error;
}

+ (FYError *)errorWithOperation:(NSURLSessionDataTask*)operation Message:(NSString *)msg {
    
    FYError* error = [FYError errorWithOperation:operation];
    
    if (msg)
        error.errorMessage = msg;
    
    return error;
}
@end
